<?php
namespace Home\Controller;
use Think\Controller;
class UserController extends Controller {
    public function index(){
        // $this->show('<style type="text/css">*{ padding: 0; margin: 0; } div{ padding: 4px 48px;} body{ background: #fff; font-family: "微软雅黑"; color: #333;font-size:24px} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.8em; font-size: 36px } a,a:hover,{color:blue;}</style><div style="padding: 24px 48px;"> <h1>:)</h1><p>欢迎使用 <b>ThinkPHP</b>！</p><br/>版本 V{$Think.version}</div><script type="text/javascript" src="http://ad.topthink.com/Public/static/client.js"></script><thinkad id="ad_55e75dfae343f5a1"></thinkad><script type="text/javascript" src="http://tajs.qq.com/stats?sId=9347272" charset="UTF-8"></script>','utf-8');
  		$this->display();  

    }

    public function verify(){ 
        $Verify = new \Think\Verify();  
        $Verify->fontSize = 18;  
        $Verify->length   = 4;  
        $Verify->useNoise = false;  
        $Verify->codeSet = '0123456789';  
        $Verify->imageW = 130;  
        $Verify->imageH = 38;  
        $Verify->entry(2); 
    }


    // $user=M('admin');
    //         // $_POST['password']=($_POST[password]);
    //          // var_dump($_POST);
    //          $userinfo['admin_name']=$_POST['username'];
    //          $userinfo['admin_password']=$_POST['password'];

    //         if($rst=$user->where($userinfo)->find()){


    public function check(){
        $verify = new \Think\Verify();  
        $code=I('post.fcode');  
        $r=$verify->check($code,2);
        if(!$r){
            $this->error('验证码错误',U('index'));
        }else{
            $user=M('User');
            $userinfo['user_name']=$_POST['user_name'];
            $userinfo['user_password']=$_POST['user_password'];
            // $_POST['user_password']=I('post.user_password');
            // var_dump($_POST);
            // var_dump($userinfo);

            if($rst=$user->where($userinfo)->find()){

                // var_dump($_POST);
                // echo $user->getLastSql();
                // $user->where($userinfo)->setInc('user_consumption_integral',100); // 用户的积分加$value
                //     echo $user->getLastSql();

                $_SESSION['login']=1;
                $_SESSION['userid']=$user->id;
                $_SESSION['username']=I('post.user_name');
                $user->user_last_login_ip=get_client_ip(); 
                $user->user_login_num=$user->user_login_num+1;
                $user->save();              
                $this->success('登录成功',U('Index/index'),10);
                
            }else{
                // echo $user->getLastSql();
                $this->error('用户名或密码有误',U('index'));
            } 
        }
    }


    public function registe(){


        $this->display();  
       
    }

    public function insert(){ 
        $user = D("User"); // 实例化User对象
        // $_POST['user_password']=md5(I('post.user_password'));
        // $_POST['repassword']=md5(I('post.repassword'));
        if (!$user->create()){
             // 如果创建失败 表示验证没有通过 输出错误提示信息

            // echo"<pre>";
            // print_r($user->getError());
            // echo "</pre>";
            $this->ajaxReturn($user->getError());
        }else{
             // 验证通过 可以进行其他数据操作
            //初始化用户数据

            
            // $user->creat_time=time();

            //  $data->time=date("Y-m-d H:i:s" ,time());user_login_num
            $user->user_login_num=1; 
            $user->user_login_ip=get_client_ip(); 
            $user->user_last_login_ip=$user->user_login_ip;
            $user->user_credit_score=0; 
            $user->user_credit_rating=1; 
            $user->user_consumption_integral=0;
            $user->user_Integral_grade=1; 
            $user->user_authentication=0;
            $user->user_is_status=1; 
            $user->user_allow_inform=1;
            $user->user_is_buy=1; 
            $user->user_allow_discuss=1; 

            $user->user_registertime=date("Y-m-d H:i:s" ,time());
            $user->user_lastlogintime=$user->user_registertime;
            if($user->add()){
                $_SESSION['login']=1;
                $_SESSION['username']=I('post.user_name');
                $_SESSION['admin']=0;
                $this->success('添加成功,将会跳转到主页',U('Index/index'),2);
            }
        }




        // $user=M('User');
        // $date=$user->create();
        // $user->user_name=I('post.username');
        // $user->user_password=md5(I('post.inputPassword'));
        // $user->user_telephone=I('post.telephone');        
        // $user->user_registertime=time();
        // $user->user_lastlogintime=$user->user_registertime;
        // if($user->add()){
        //     $_SESSION['login']=1;
        //     $_SESSION['username']=I('post.username');
        //     $this->success('添加成功,将会跳转到主页',U('Index/index'),2);
        // }
    }


    public function logout(){
        setcookie(session_name(),'',time()-3600,'/');
        session_unset();
        session_destroy();

        $this->success('退出成功',U('Index/index'));
    }




    public function service(){

    	$this->display();  

    }
     public function addshop(){

    	$this->display();  

    } 

    public function add_user_consumption_integral($value){
        // $user_name=$_SESSION['username'];
        // $user = M("User"); // 实例化User对象
        // // 要修改的数据对象属性赋值
        //  var_dump($user);
        //  if($user){
        //     $User->where('user_name=$user_name')->find();


        //  }



            $user=M('User');
            $user_name=$_SESSION['username']; 

            $user->where("user_name=$user_name")->setInc('user_consumption_integral',$value); // 用户的积分加$value
            


         // $List = $Dao->where('uid<10 AND email="Jack@163.com"')->find();
          

        // $User->where('user_name=$user_name')->setInc('user_consumption_integral',$value); // 用户的积分加$value
        // var_dump($this->getLastSql());

        // $user->user_consumption_integral = $user->user_consumption_integral+50;
                /////////////更新用户积分等级$user->user_Integral_grade=
       
       

    }


}
